<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/19
 * Time: 10:38
 */

namespace app\course\controller;


use app\course\service\CourseTeacherStatisticsService;
use cmf\controller\AdminBaseController;
use PHPExcel;
use think\Db;


class CourseTeacherStatisticsController extends AdminBaseController
{
    public function index()
    {
        $param = $this->request->param();
        $param['admin_shop_id'] = $this->admin_shop_id;

        $service = new CourseTeacherStatisticsService();
        $data = $service->getList($param);

        $this->assign('param', $param);
        $this->assign('data', $data);
        $this->assign('page', $data->render());
        return $this->fetch();
    }

    public function classList()
    {
        $tid = $this->request->param('tid');
        $param = $this->request->param();

        $service = new CourseTeacherStatisticsService();
        $data = $service->getClassList($tid, $param);
        $this->assign('param', $param);
        $this->assign('page', $data->render());
        $this->assign('data', $data);
        return $this->fetch('classlist');
    }

    public function courseList()
    {
        $tid = $this->request->param('tid');
        $service = new CourseTeacherStatisticsService();
        $data = $service->getCourseList($tid);
//        dump($data);
        $this->assign('page', $data->render());
        //重新排序
        foreach ($data as $k => $v) {
            $j = $k + 1;
            foreach ($data as $j => $val) {
                if (isset($data[$j])) {
                    if(!empty($val['supply_teacher']))
                    {
                        $data[$k]['supply_teacher']=Db::name('user')->where(['id'=>$val['supply_teacher']])->value('user_login');
                    }
                    $before = $data[$k]['date'];
                    $after = $data[$j]['date'];
                    if ($before < $after) {
                        $t = $data[$j];
                        $data[$j] = $data[$k];
                        $data[$k] = $t;
                    }
                }
                $teacher_id = $data[$k]['teacher_id'];
            }
        }
        $this->assign('teacher_id', $teacher_id);
        $this->assign('data', $data);
        return $this->fetch('courselist');
    }

    public function export()
    {
        $tid = $this->request->param('teacher_id');
        $service = new CourseTeacherStatisticsService();
        $data = $service->getCourseListExe($tid);
        foreach ($data as $k => $v) {
            $j = $k + 1;
            foreach ($data as $j => $val) {
                if (isset($data[$j])) {
                    $before = $data[$k]['date'];
                    $after = $data[$j]['date'];
                    if ($before < $after) {
                        $t = $data[$j];
                        $data[$j] = $data[$k];
                        $data[$k] = $t;
                    }
                }
            }
        }
        import('phpexcel.PHPExcel', EXTEND_PATH);
        import('phpexcel.PHPExcel.IOFactory', EXTEND_PATH);
        import('phpexcel.PHPExcel.Shared',EXTEND_PATH);




        $objPHPExcel = new PHPExcel();
        $objSheet = $objPHPExcel->getActiveSheet();//获取当前活动sheet
        $objSheet->setTitle("老师课程安排表");//给当前活动sheet起个名称
        // Set properties
        $objPHPExcel->getProperties()->setCreator("ctos")
            ->setLastModifiedBy("ctos")
            ->setTitle("Office 2007 XLSX Test Document")
            ->setSubject("Office 2007 XLSX Test Document")
            ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
            ->setKeywords("office 2007 openxml php")
            ->setCategory("Test result file");

        //设置列宽
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(35);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(35);

        $objPHPExcel->getActiveSheet()->getStyle('A1:M1')->getFont()->setSize(14)->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('A2:M220')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        $objSheet->setCellValue("A1", "校区")->setCellValue("B1", "课程")->setCellValue("C1", "班级")
            ->setCellValue("D1", "课时")->setCellValue("E1", "上课时间")->setCellValue("F1", "上课的开始")
            ->setCellValue("G1", "上课的结束")->setCellValue("H1", "教室")->setCellValue('J1','老师');//填充数据
        $j = 2;
        //时间格式
        $phpexcel_date_format = array(\PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD, \PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2, \PHPExcel_Style_NumberFormat::FORMAT_DATE_DDMMYYYY, \PHPExcel_Style_NumberFormat::FORMAT_DATE_DMYSLASH, \PHPExcel_Style_NumberFormat::FORMAT_DATE_DMYMINUS, \PHPExcel_Style_NumberFormat::FORMAT_DATE_DMMINUS, \PHPExcel_Style_NumberFormat::FORMAT_DATE_MYMINUS, \PHPExcel_Style_NumberFormat::FORMAT_DATE_DATETIME, \PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME1, \PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME2, \PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME3, \PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4, \PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME5, \PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME6, \PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME7, \PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME8, \PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH, \PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX14, \PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15, \PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX16, \PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX17, \PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX22);
        foreach ($data as $key => $val) {
            if(!empty($val['supply_teacher']))
            {
                $val['uname']=Db::name('user')->where(['id'=>$val['supply_teacher']])->value('user_login');//代课老师
            }
            $objSheet->setCellValue("A" . $j, $val['sname'])->setCellValue("B" . $j, $val['course_name'])->setCellValue("C" . $j, $val['pname'])
                ->setCellValue("D" . $j, $val['schedule'])->setCellValue("E" . $j,(strtotime($val['date'])))->setCellValue("F" . $j,$val['start_time'])
                ->setCellValue("G" . $j, $val['end_time'])->setCellValue("H" . $j, $val['rname'])
                ->setCellValue("J" . $j, $val['uname']);
//            $objSheet->getStyle('E'.$j)->getNumberFormat()->setFormatCode($phpexcel_date_format[1]);

            $objSheet->setCellValue('E'.($j), \PHPExcel_Shared_Date::PHPToExcel(strtotime('+1 day',$val['date'])));
//            $objSheet->setCellValue('E'.($j), \PHPExcel_Shared_Date::PHPToExcel(time()));
            $objSheet->getStyle('E'.($j))->getNumberFormat()->setFormatCode($phpexcel_date_format[1]);
            $j++;
        }
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');//生成excel文件
        //$objWriter->save($dir."/export_1.xls");//保存文件

        // Set active sheet index to the first sheet
        $objPHPExcel->setActiveSheetIndex(0);

        ob_end_clean();//清除缓冲区,避免乱码

        // Redirect output to a client’s web browser (Excel5)
        header('Content-Type: application/vnd.ms-excel');
        $filename = '班级导出' . '(' . date('Ymd-His') . ').xls';
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');


    }
    public function browser_export($type, $filename)
    {
        if ($type == "Excel5") {
            header('Content-Type: application/vnd.ms-excel');//告诉浏览器将要输出excel03文件
        } else {
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//告诉浏览器数据excel07文件
        }
        header('Content-Disposition: attachment;filename="' . $filename . '"');//告诉浏览器将输出文件的名称
        header('Cache-Control: max-age=0');//禁止缓存
    }
}

